home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / prog_c / cuj0896.zip / WAMPLER.ZIP / DRAWCNV.CXX < prev    next >
C/C++ Source or Header  |  1996-05-24  |  5KB  |  157 lines

  1. //================================================================
  2. //  drawcnv.cxx -- myCanvasPane class defintion
  3. //  Copyright (C) 1995  Bruce E. Wampler
  4. //
  5. //  This SIMPLE canvas class just draws lines using the pen that
  6. //  goes with this canvas. It doesn't handle any scrolling or
  7. //  paging. It also only handles up to 200 lines.
  8. //================================================================
  9.  
  10. #include "drawcnv.h"
  11.  
  12. //================>>> myCanvasPane::myCanvasPane <<<==============
  13. myCanvasPane::myCanvasPane()
  14. {
  15.   // Initialize to known values
  16.   _mouseDown = 0;
  17.   _begx = -1;  _begy = -1;  _curx = -1;  _cury = -1;
  18.   _pt = new point[200];    // Just 200 lines for this example
  19.   _nextpt = 0;
  20. }
  21.  
  22. //==============>>> myCanvasPane::myCanvasPane <<<================
  23. myCanvasPane::~myCanvasPane()
  24. {
  25.   delete [] _pt;           // free the point array
  26. }
  27.  
  28. //=================>>> myCanvasPane::Clear <<<====================
  29. void myCanvasPane::Clear()
  30. {
  31.   vCanvasPane::Clear();       // clear the canvas
  32.   _nextpt = 0;                // and all the points
  33. }
  34.  
  35. //================>>> myCanvasPane::MouseDown <<<=================
  36. void myCanvasPane::MouseDown(int X, int Y, int button)
  37. {
  38.   // Note beginning of line on mouse down
  39.   _mouseDown = 1;
  40.   _begx = _curx = X;    _begy = _cury = Y;
  41.  
  42.   _pt[_nextpt].x = X;  _pt[_nextpt].y = Y;
  43.   _pt[_nextpt].pPen = GetPen();
  44.   ++_nextpt;
  45.   if (_nextpt >= 200)         // really dumb, but SIMPLE!
  46.       _nextpt = 0;
  47. }
  48.  
  49. //==================>>> myCanvasPane::MouseUp <<<=================
  50. void myCanvasPane::MouseUp(int X, int Y, int button)
  51. {
  52.   // Finish drawing line on mouse up
  53.   _mouseDown = 0;
  54.   if (_begx != X || _begy != Y)  // First time?
  55.       DrawLine(_begx, _begy, X, Y);
  56.  
  57.   _pt[_nextpt].x = X;    _pt[_nextpt].y = Y;
  58.   _pt[_nextpt].pPen = GetPen();
  59.   ++_nextpt;
  60.    if (_nextpt >= 200)         // really dumb, but SIMPLE!
  61.       _nextpt = 0;
  62.  
  63.   _mouseDown = 0;             // ready for next line
  64.   _begx = -1;  _begy = -1;  _curx = -1;  _cury = -1;
  65. }
  66.  
  67. //==================>>> myCanvasPane::MouseMove <<<===============
  68. void myCanvasPane::MouseMove(int x, int y, int button)
  69. {
  70.   // Draw rubber band line on mouse move
  71.   if (_begx != _curx || _begy != _cury)    // old line to clear?
  72.       DrawRubberLine(_begx, _begy, _curx, _cury);
  73.  
  74.   if (_begx != x || _begy != y)           // draw new line
  75.       DrawRubberLine(_begx, _begy, x, y);
  76.     
  77.   _curx = x;    _cury = y;                // update
  78. }
  79.  
  80. //===================>>> myCanvasPane::Redraw <<<=================
  81. void myCanvasPane::Redraw(int x, int y, int w, int h)
  82. {
  83.   // This is a stupid Redraw that just redraws everything.
  84.   // It also starts losing things after 200 points. This
  85.   // is just sample code, remember!
  86.  
  87.   int x1, y1, x2, y2;         // work variables
  88.  
  89.   for (int ix = 0 ; ix < _nextpt ; ix += 2)
  90.     {
  91.       if (ix == 0 || _pt[ix].pPen != _pt[ix-2].pPen)
  92.           SetPen(_pt[ix].pPen);
  93.       x1 = _pt[ix].x;      y1 = _pt[ix].y;
  94.       x2 = _pt[ix+1].x;    y2 = _pt[ix+1].y;
  95.       DrawLine(x1, y1, x2, y2);
  96.     }
  97. }
  98.  
  99. //==================>>> myCanvasPane::Resize <<<==================
  100. void myCanvasPane::Resize(int w, int h)
  101. {
  102.   // This simple example doesn't need to do anything special for
  103.   // resize. The default will cause a Redraw.
  104.   vCanvasPane::Resize(w,h);
  105. }
  106.  
  107. #include <fstream.h>        // to save/restore drawings
  108.  
  109. //==================>>> myCanvasPane::Read <<<====================
  110. int myCanvasPane::Read(char* name)
  111. {
  112.   // Read in a file of points
  113.   int r,g,b,ps;
  114.  
  115.   if (!name || !*name) return 0;      // sanity check
  116.   ifstream inFile(name);              // open the file to read
  117.  
  118.   if (!inFile) return 0;              // OK?
  119.     
  120.   inFile >> _nextpt;                  // number of points
  121.   for (int ix = 0 ; ix < _nextpt ; ++ix)  // read the points
  122.     {
  123.       inFile >> _pt[ix].x >> _pt[ix].y >> r >> g >> b
  124.           >> _pt[ix].pPen.penWidth >> ps;
  125.         _pt[ix].pPen.penColor.Set(r,g,b); // can't read directly
  126.         _pt[ix].pPen.penStyle = (PenStyle) ps;
  127.     }
  128.   inFile.close();                     // finished with file
  129.   return 1;
  130. }
  131.  
  132. //==================>>> myCanvasPane::Save <<<====================
  133. int myCanvasPane::Save(char* name)
  134. {
  135.   // Save drawing to a file of points
  136.  
  137.   if (!name || !*name) return 0;      // sanity check
  138.   ofstream outFile(name);             // open output file
  139.  
  140.   if (!outFile) return 0;             // OK?
  141.  
  142.   outFile << _nextpt << endl;         // number of points
  143.   for (int ix = 0 ; ix < _nextpt ; ++ix) // write the points
  144.     {
  145.       outFile << _pt[ix].x << " " << _pt[ix].y << " "
  146.               << _pt[ix].pPen.penColor.r() << " "
  147.               << _pt[ix].pPen.penColor.g() << " "
  148.               << _pt[ix].pPen.penColor.b() << " "
  149.               << _pt[ix].pPen.penWidth << " "
  150.               << _pt[ix].pPen.penStyle << endl;
  151.     }
  152.   outFile.close();                    // done with file
  153.   return 1;
  154. }
  155.  
  156.  
  157.